#include <iostream>
#include <fstream>
#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"
#include "TSpectrum.h"
#include "TMath.h"
#include "TSystem.h"
#include "TPostScript.h"
#include "TTimeStamp.h"
#include "TSpectrum.h"
#include "iostream"

#include "trackHandle.h"

using namespace std;

int main()
{
    // 100为ADC阈值
    TrackHandle TH(10);
    vector<myTrack> Vtrack;
    TFile *tfile = new TFile("Feature.root","recreate"); 
    TTree *t_data = new TTree("t_data","data feature result");

    // 处理单个数据文件
    // string filename = "outframe.dat";
    // TH.calculate(filename, Vtrack);
    // cout << Vtrack.size() << endl;
    // ifstream input;
    // input.open(filename, ios::binary);

    // 批量处理指定目录下多文件
    string folder = "/public/home/zhangj/data/fenghb2/xieyj/dat";
    TH.calculateFromFolder(folder, Vtrack);
    ifstream input;
    input.open(Vtrack[0].filename,ios::binary);
    cout << Vtrack.size() << endl;

    //UShort_t data[72][72];  t_data->Branch("data",&data,"data[72][72]/s");  //for unsigned short
    Short_t data[72][72];   t_data->Branch("data",&data,"data[72][72]/S");
    Int_t numEvent;         t_data->Branch("numEvent",&numEvent,"numEvent/I");
    Int_t numFrame;         t_data->Branch("numFrame",&numFrame,"numFrame/I");
    Int_t timeYear;         t_data->Branch("timeYear",&timeYear,"timeYear/I");
    Int_t timeMon;          t_data->Branch("timeMon",&timeMon,"timeMon/I");
    Int_t timeMday;         t_data->Branch("timeMday",&timeMday,"timeMday/I");
    Int_t timeHour;         t_data->Branch("timeHour",&timeHour,"timeHour/I");
    Int_t timeMin;          t_data->Branch("timeMin",&timeMin,"timeMin/I");
    Int_t timeSec;          t_data->Branch("timeSec",&timeSec,"timeSec/I");
    Int_t timeNsec;         t_data->Branch("timeNsec",&timeNsec,"timeNsec/I");
    Int_t peakADC;          t_data->Branch("peakADC",&peakADC,"peakADC/I");
    Double_t peakX;         t_data->Branch("peakX",&peakX,"peakX/D");
    Double_t peakY;         t_data->Branch("peakY",&peakY,"peakY/D");
    Int_t sumADC;           t_data->Branch("sumADC",&sumADC,"sumADC/I");
    Int_t sumHit;           t_data->Branch("sumHit",&sumHit,"sumHit/I");
    Double_t lenXY;         t_data->Branch("lenXY",&lenXY,"lenXY/D");
    Double_t ratioXY;       t_data->Branch("ratioXY",&ratioXY,"ratioXY/D");
    Double_t BcenterX;      t_data->Branch("BcenterX",&BcenterX,"BcenterX/D");
    Double_t BcenterY;      t_data->Branch("BcenterY",&BcenterY,"BcenterY/D");
    Double_t lenBtoP;       t_data->Branch("lenBtoP",&lenBtoP,"lenBtoP/D");
    Int_t length;           t_data->Branch("length",&length,"length/I");
    Double_t width;         t_data->Branch("width",&width,"width/D");
    Int_t outline;          t_data->Branch("outline",&outline,"outline/I");
    Int_t edgeHit;          t_data->Branch("edgeHit",&edgeHit,"edgeHit/I");
    
    string lastFielName = Vtrack[0].filename;
    cout << lastFielName << endl;
    // test
    //cout << Vtrack[0].topTime.year << endl;
    for(int i =0; i < Vtrack.size(); i ++)
    {
        if(Vtrack[i].filename != lastFielName)
        {
            input.close();
            input.open(Vtrack[i].filename, ios::binary);
            cout << Vtrack[i].filename << endl;
        }
        for(int lin = 0; lin < 72; lin ++){
            for(int row = 0; row < 72; row ++){
                //data[lin][row] = Vtrack[i].data[lin][row];
                input.read((char *)(&data[lin][row]), sizeof(data[lin][row]));
                //if(data[lin][row] > 10 && i == 1)cout << data[lin][row] << endl;
            }
        }
        numEvent = Vtrack[i].numEvent;
        numFrame = Vtrack[i].numFrame;
        timeYear = Vtrack[i].topTime.year;
        timeMon = Vtrack[i].topTime.mon;
        timeMday = Vtrack[i].topTime.mday;
        timeMin = Vtrack[i].topTime.min;
        timeSec = Vtrack[i].topTime.sec;
        timeNsec = Vtrack[i].topTime.nsec;
        peakADC = Vtrack[i].peakADC;
        peakX = Vtrack[i].Peak.x;
        peakY = Vtrack[i].Peak.y;
        sumADC = Vtrack[i].sumADC;
        sumHit = Vtrack[i].sumHit;
        lenXY = Vtrack[i].lenXY;
        ratioXY = Vtrack[i].ratioXY;
        BcenterX = Vtrack[i].Bcenter.x;
        BcenterY = Vtrack[i].Bcenter.y;
        lenBtoP = Vtrack[i].lenBtoP;
        length = Vtrack[i].length;
        width = Vtrack[i].width;
        outline = Vtrack[i].outline;
        edgeHit = Vtrack[i].edgeHit;
        t_data->Fill();
        // if(i < 10) cout << "id: " << i 
        //                 << " sumHit: " << Vtrack[i].sumHit 
        //                 << " length: " << Vtrack[i].length 
        //                 << " outline: " << Vtrack[i].outline 
        //                 << " width: " << Vtrack[i].width
        //                 << " numEvent: " << numEvent
        //                 << " numFrame: " << numFrame
        //                 << endl;
        // if(i > 1000) break;
        lastFielName = Vtrack[i].filename;
    }
    //t_data->Print();
    tfile->cd();
    tfile->Write();
    tfile->Close();

    cout << "File Fearture Extraction Completed" << endl;
    return 0;
}
